﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace ImageRecognition2
{
    /// <summary>
    /// Assembly Helper Tool
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class AssemblyHelperTool<T>
    {
        private List<T> m_aT;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_sNameSpace"></param>
        /// <param name="_sEndsWidth"></param>
        public AssemblyHelperTool(string _sNameSpace, string _sEndsWidth, object[] _aObject)
        {
            this.m_aT = new List<T>();
            Type[] aType = Assembly.Load(Assembly.GetExecutingAssembly().GetName()).GetTypes();
            this.CreateInstances(aType, _sNameSpace, _sEndsWidth, _aObject);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_sNameSpace"></param>
        /// <param name="_type"></param>
        public AssemblyHelperTool(string _sNameSpace, Type _type)
        {
            this.m_aT = new List<T>();
            Type[] aType = Assembly.Load(Assembly.GetExecutingAssembly().GetName()).GetTypes();
        }

        /// <summary>
        /// Create Instances
        /// </summary>
        /// <param name="aType"></param>
        /// <param name="_sNameSpace"></param>
        /// <param name="_sEndsWith"></param>
        /// <param name="_aObject"></param>
        private void CreateInstances(Type[] aType, string _sNameSpace, string _sEndsWith, object[] _aObject)
        {
            for (int i = 0; i < aType.Length; i++)
            {
                string sType = aType[i].FullName;
                if ((sType.StartsWith(_sNameSpace) && !aType[i].IsAbstract) && sType.EndsWith(_sEndsWith))
                {
                    this.m_aT.Add((T)Activator.CreateInstance(aType[i], _aObject));
                }
            }
        }

        /// <summary>
        /// Get Item
        /// </summary>
        /// <param name="_nIndex"></param>
        /// <returns></returns>
        public T this[int _nIndex]
        {
            get { return this.m_aT[_nIndex]; }
        }

        /// <summary>
        /// Get Items
        /// </summary>
        public List<T> Items
        {
            get { return this.m_aT; }
        }
    }
}

